#delimit;
clear all;
set more off;



use "voter_file_hist_all_imp.dta";


replace y2020_001_party  = "NA" if y2020_001_party == "";


collapse (mean) mean_trumpapp = pres_app_bin_ga_rv_extend (sd) sd_trumpapp =pres_app_bin_ga_rv_extend (count) n_trumpapp =pres_app_bin_ga_rv_extend, by(y2020_001_party);


generate up_ci = mean_trumpapp + invttail(n_trumpapp-1,0.0005)*(sd_trumpapp / sqrt(n_trumpapp));
generate low_ci = mean_trumpapp - invttail(n_trumpapp-1,0.0005)*(sd_trumpapp / sqrt(n_trumpapp));


gen x_lab = 1 if y2020_001_party == "D ";
replace x_lab = 2 if y2020_001_party == "NP";
replace x_lab = 3 if y2020_001_party == "R ";
replace x_lab = 4 if y2020_001_party == "NA";


twoway (bar mean_trumpapp x_lab,  color(%90) barw(0.75) bargap(-100)) (rcap up_ci low_ci x_lab), legend(off) xtitle("2020 Primary Participation") yscale(range(0 1)) ylabel(0(0.2) 1)
xlabel(1 "Democratic Primary"  2 "Non-Partisan" 3 "Republican Primary" 4 "Didn't Vote") ytitle("Share of Estimated Trump Approvers");



graph export "Stacked_Bar_ByParty_PB.pdf", replace;


clear all; 
shell /Library/Frameworks/R.framework/Resources/Rscript oos_plots.R;


#delimit;
clear all;
import delimited "Prec_OOS.csv";

destring pres_app_bin_ga_rv_extend, replace force;

destring pres_app_bin_ga_rv_extend_gen_re, replace force;

sum statewide_mean;

local state_mean = round(`r(mean)', 0.0001);



graph twoway (scatter pr_trump_2pshare pres_app_bin_ga_rv_extend) (function y = x, lpattern(dash)), aspect(1) xtitle("Predicted Precinct-Level Trump Approver Share")
ytitle("Precinct-Level Trump Vote Share") legend(off) note(`=ustrunescape("\u03BC\u0302")'=`:display %9.3f `state_mean'',  size(medlarge)  pos(10) ring(0)) saving(gr_1.gph, replace);


sum statewide_reclass_mean;

local state_rec_mean = round(`r(mean)', 0.0001);

#delimit;
graph twoway (scatter pr_trump_2pshare pres_app_bin_ga_rv_extend_gen_re) (function y = x, lpattern(dash)), aspect(1) xtitle("Predicted Precinct-Level Trump Approver Share")
ytitle("Precinct-Level Trump Vote Share") legend(off) note(`=ustrunescape("\u03BC\u0302")'=`:display %9.3f `state_rec_mean'', size(medlarge) pos(10) ring(0)) saving(gr_2.gph, replace);



corr pr_trump_2pshare pres_app_bin_ga_rv_extend_gen_re;

regress  pr_trump_2pshare pres_app_bin_ga_rv_extend_gen_re, robust;

gr combine gr_1.gph gr_2.gph;


graph export "Prec_OOS_Comb_PB.pdf", replace;
